//
//  ConversationDetailController.m
//  LookScore
//
//  Created by wws on 2018/1/22.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "ConversationDetailCell.h"
#import "ConversationCommentCell.h"
#import "ConversationDetailController.h"
#import "LSConversationDetailSectionHeaderView.h"
#import "LSChatKeyboard.h"
#import <IQKeyboardManager.h>
#import "UITableViewCell+HYBMasonryAutoCellHeight.h"
#import "LSSecondCommentCell.h"
#import "LSOtherHomePageViewController.h"
#import "LSPersonalHomePageVController.h"
#import "DXShareView.h"
@interface ConversationDetailController ()<UITableViewDataSource, UITableViewDelegate,ConversationCommentCellDelegate,ConversationDetailCellDelegate>{
    
    UITableView            *_tableView;//列表
    ConversationIssueModel *_issueModel;//话题模型
    
    NSInteger               _pageCount;
    
    NSInteger              _type;//查询类型
    
    ConversationIssueModel * _model;
    
    
    NSInteger _topicId;
    NSString * _beCid;
    NSInteger _schemeId;
    
    
}
@property(strong,nonatomic)NSMutableArray *conversModelArray;//详情模型数组
@property (nonatomic, strong) NSMutableArray * commentArray;//评论模型数组
@property (nonatomic, strong) LSChatKeyboard * customView;
@property (nonatomic, strong) NSMutableArray * commentArray1;

@property (nonatomic, assign) CGFloat history_Y_offset;

@property (nonatomic, assign) CGFloat seletedCellHeight;

@property (nonatomic, assign) NSInteger secPageCount;

@property (nonatomic, strong) NSIndexPath * scrollIndexPath;
@end


@implementation ConversationDetailController{
    MenuView * operationView;
    dispatch_group_t group;
    
}


- (LSChatKeyboard *)customView{
    if (!_customView) {
        _customView = [[LSChatKeyboard alloc]init];
        _customView.msgTextView.placeholder = @"评论楼主...";
        _customView.frame = CGRectMake(0, LSScreenHeight-45-LSNavBarHeight-HOME_INDICATOR_HEIGHT, LSScreenWidth, 273);
        KWeakSelf(self);
        _customView.block = ^{
            LSLoginVController *loginVC=[[LSLoginVController alloc]init];
            loginVC.presentVCName = @"ConverSationVC";
            [weakself presentRootViewController:loginVC animated:YES completion:nil];
            loginVC.loginSuccessBlock = ^{
              [weakself hideKeyboard];
            };
        };
        //传入当前控制器 ，方便打开相册（如放到控制器 ， 后期的逻辑过多，控制器会更加臃肿）
        
        [_customView textCallback:^(NSString *text) {
            [weakself sendTextMessage:text];
        } target:self];
    }
    
    return _customView;
}

- (NSMutableArray *)conversModelArray{
    if (!_conversModelArray) {
        _conversModelArray = @[].mutableCopy;
    }
    
    return _conversModelArray;
}

- (NSMutableArray *)commentArray{
    if (!_commentArray) {
        _commentArray = @[].mutableCopy;
    }
    return _commentArray;
}


- (NSMutableArray *)commentArray1{
    
    if (!_commentArray1) {
        _commentArray1 = @[].mutableCopy;
    }
    
    return _commentArray1;
}
- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.translucent = NO;
    //系统键盘弹起通知
    [[IQKeyboardManager sharedManager] setEnable:NO];
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
    
    [notifiCenter addObserver:self.customView selector:@selector(systemKeyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    
    //    [[NSNotificationCenter defaultCenter]addObserver:self.customView selector:@selector(keyboardResignFirstResponder:) name:@"ChatKeyboardShouldResignFirstResponder" object:nil];
    [notifiCenter addObserver:self.customView selector:@selector(keyboardResignFirstResponder:) name:UIKeyboardWillHideNotification object:nil];
    [self hideKeyboard];
    if (@available(iOS 11.0, *)) {
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
    }
    
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [notifiCenter removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [notifiCenter removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}


- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[IQKeyboardManager sharedManager] setEnable:YES];
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"话题详情";
  
    self.scrollIndexPath = [[NSIndexPath alloc] init];
    
    //加入子视图
    [self addConversationDetailSubviews];
    
    [self.view addSubview:self.customView];
    _type = 1;
    group = dispatch_group_create();
    
    _tableView.mj_header =  [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_group_enter(group);
        [self loadData:YES];
        dispatch_group_enter(group);
        [self loadCommentData:YES withType:_type withIsFirstLoad:YES];
        
        
        dispatch_group_notify(group, dispatch_get_main_queue(), ^{
            //等待requset1， requset2均调用dispatch_group_leave则进入dispatch_group_notify
            [_tableView reloadData];
        });
    }];
    [_tableView.mj_header beginRefreshing];
    
    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self loadCommentData:NO withType:_type withIsFirstLoad:NO];
    }];
    
   
    
    _tableView.mj_footer.ignoredScrollViewContentInsetBottom = HOME_INDICATOR_HEIGHT;
}


/*加入子视图*/
-(void)addConversationDetailSubviews{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, LSScreenWidth,LSScreenHeight - 45-LSNavBarHeight) style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.showsVerticalScrollIndicator=NO;
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    _tableView=tableView;
    [self.view addSubview:tableView];
  
    
    //右边的分享按钮
//    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"share_issue_white"] style:UIBarButtonItemStylePlain target:self action:@selector(shareIssueDetailSender:)];
//    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16]} forState:UIControlStateNormal];
    
 
}
- (void)loadData:(BOOL)refresh{
    
        [LSHttpEnclosureTool GET:LSTOPICDETAIL parameters:@{@"id":@(self.TopicId),@"cid":self.cid} resBlock:^(id responseObject) {
            [self.conversModelArray removeAllObjects];
            if (responseObject) {
                _model = [ConversationIssueModel mj_objectWithKeyValues:responseObject];
                _topicId = _model.TopicId;
                _beCid = _model.UserCid;
                [self.conversModelArray addObject:_model];
            }
            if (!refresh) {
                [_tableView reloadData];
            }
            //        [_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
            dispatch_group_leave(group);
        }];
    
}

- (void)loadCommentData:(BOOL)refresh withType:(NSInteger)type withIsFirstLoad:(BOOL)firstLoad{
    if (refresh) {
        _pageCount = 0;
    }else{
        _pageCount += 1;
    }
    [LSHttpEnclosureTool GET:LSTOPCOMMENTLISt parameters:@{@"tid":@(self.TopicId),@"pageIndex":@(_pageCount)} resBlock:^(id responseObject) {
        if (refresh) {
            [self.commentArray removeAllObjects];
        }

        if (responseObject) {
            for (NSDictionary * dict in responseObject[@"CommentList"]) {
                CommentIssueModel * model = [CommentIssueModel mj_objectWithKeyValues:dict];
                model.pageCount = 3;
                model.shouldUpdateCache = YES;
                [self.commentArray addObject:model];
            }
            
        }else{
                
            _pageCount -= 1;
        }
           
//            if (scrollCurrent) {
//
//
////                [_tableView scrollToRowAtIndexPath:self.scrollIndexPath atScrollPosition:UITableViewScrollPositionNone animated:NO];
////                []
//            }
        [_tableView reloadData];
        [CommonClass endRefreshing:_tableView];
        if (firstLoad) {
            dispatch_group_leave(group);
        }
            
    }];
        
    
}

#pragma mark - UITableViewDelegate,UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section==0) {
        return self.conversModelArray.count;
    }else{
        return self.commentArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        ConversationDetailCell *cell=[ConversationDetailCell ConversationDetailCellWidthTableView:tableView];
        cell.delegate = self;
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        ConversationIssueModel * model = self.conversModelArray[indexPath.row];
        cell.issueModel = model;
        cell.refreshBlock = ^{
            MyLog(@"111");
//            [_tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationNone];
            [_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
//            [_tableView reloadData];
        };
        cell.block = ^{
            if ([LSAccountArchive isLogin]) {
                if ([[LSAccountArchive account].Cid isEqualToString:model.UserCid]) {
                    LSPersonalHomePageVController * personalVc = [[LSPersonalHomePageVController alloc] init];
                    personalVc.userCid = model.UserCid;
                    [self.navigationController pushViewController:personalVc animated:YES];
                }else{
                    LSOtherHomePageViewController * otherVC = [[LSOtherHomePageViewController alloc] init];
                    otherVC.userCid = model.UserCid;
                    [self.navigationController pushViewController:otherVC animated:YES];
                }
            }else{
                [self presentLoginVC];
            }
           
        };
        return cell;
    }else{
        ConversationCommentCell *cell = [ConversationCommentCell ConversationCommentCellWidthTableView:tableView];
        cell.delegate = self;
//        [cell useCellFrameCacheWithIndexPath:indexPath tableView:tableView];
        CommentIssueModel * model = self.commentArray[indexPath.row];
        cell.commentModel = self.commentArray[indexPath.row];
//        cell.commentArray = self.commentArray1;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.converModel = _model;
        cell.myIndexPath = indexPath;
//         KWeakSelf(self);
        cell.block = ^(UIButton *btn, NSIndexPath *indexPath) {
            model.shouldUpdateCache = YES;
            model.isExpand = !model.isExpand;
            model.pageCount = model.SecondCommentList.count;
//            btn.hidden = YES;5
            [_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            
//            [weakself load1CommentData:NO withType:2 with:model.TopicId with:model.Id WithIndexPath:indexPath withModel:model withPageCount:model.pageCount];
        };
        cell.clickedImageViewBlock = ^{
            if ([LSAccountArchive isLogin]) {
                if ([[LSAccountArchive account].Cid isEqualToString:model.UserCid]) {
                    LSPersonalHomePageVController * personalVc = [[LSPersonalHomePageVController alloc] init];
                    personalVc.userCid = model.UserCid;
                    [self.navigationController pushViewController:personalVc animated:YES];
                }else{
                    LSOtherHomePageViewController * otherVC = [[LSOtherHomePageViewController alloc] init];
                    otherVC.userCid = model.UserCid;
                    [self.navigationController pushViewController:otherVC animated:YES];
                }
            }else{
                [self presentLoginVC];
            }
        };
        return cell;
    }
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return nil;
    }
    
    UIView * headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headerView"];
    if (!headerView) {
        headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, LSScreenWidth, 40)];
        headerView.backgroundColor = LSBackGroundColor;
        LSConversationDetailSectionHeaderView * sectionHeaderView = [[LSConversationDetailSectionHeaderView alloc] initWithFrame:CGRectMake(0, 5, LSScreenWidth, 35)];
        sectionHeaderView.allCommentStr = [NSString stringWithFormat:@"(共%ld条评论)",_model.CommentCount];
        sectionHeaderView.backgroundColor = LSTitleWhiteColor;
        [headerView addSubview:sectionHeaderView];
    }
    
    if (self.commentArray.count <= 0) {
        headerView.hidden = YES;
    }
    return headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.01;
    }
    if (self.commentArray.count <= 0) {
        return 0.01;
    }
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.01;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        
        ConversationIssueModel * model = self.conversModelArray[indexPath.row];
//        MyLog(@"sectionH = %.f",[tableView cellHeightForIndexPath:indexPath model:model keyPath:@"issueModel" cellClass:[ConversationDetailCell class] contentViewWidth:[tableView cellContentViewWith]]);
        return [tableView cellHeightForIndexPath:indexPath model:model keyPath:@"issueModel" cellClass:[ConversationDetailCell class] contentViewWidth:[tableView cellContentViewWith]];
    }else{
        CommentIssueModel *commentModel = self.commentArray[indexPath.row];
        return [tableView cellHeightForIndexPath:indexPath model:commentModel keyPath:@"commentModel" cellClass:[ConversationCommentCell class] contentViewWidth:[tableView cellContentViewWith]];

    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([LSAccountArchive isLogin]) {
        if (indexPath.section == 0) {
            [self.customView.msgTextView becomeFirstResponder];
            self.customView.msgTextView.placeholder = @"评论楼主...";
            ConversationIssueModel * model = self.conversModelArray[indexPath.section];
            _topicId = model.TopicId;
            _beCid = model.UserCid;
            _schemeId = 0;
            
        }else{
            ConversationCommentCell * cell = [tableView cellForRowAtIndexPath:indexPath];
            cell.operationView.show = !cell.operationView.isShowing;
            CommentIssueModel * model = self.commentArray[indexPath.row];
            _topicId = model.TopicId;
            _beCid = model.UserCid;
            _schemeId = model.Id;
            self.customView.msgTextView.placeholder = [NSString stringWithFormat:@"评论 %@...",model.UserDisName];
            [self.customView.msgTextView becomeFirstResponder];
            UIWindow *window = [UIApplication sharedApplication].keyWindow;
            self.history_Y_offset = [cell convertRect:cell.bounds toView:window].origin.y;
            [self keyboardWillShow];
            self.seletedCellHeight = 0;
//            operationView = cell.operationView;
        }
    }else{
        
        LSLoginVController *loginVC=[[LSLoginVController alloc]init];
        loginVC.presentVCName = @"ConverSationVC";
        [self presentRootViewController:loginVC animated:YES completion:nil];
        loginVC.loginSuccessBlock = ^{
            [self hideKeyboard];
        };
        
    }
//    [self topicCommentWithTopicId:model.TopicId WithBeCid:model.BeCommentId withCid:[LSAccountArchive account].Cid withContent:@""];
    
}

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
//    ConversationCommentCell * cell = [tableView cellForRowAtIndexPath:indexPath];
//    if (operationView) {
//        if (operationView.isShowing) {
//            operationView.show = NO;
//        }
//    }
    
}

#pragma mark --ConversationCommentCellDelegate
- (void)passCellHeight:(CGFloat)cellHeight secondCommentModel:(LSSecondCommentModel *)secondCommentModel commentCell:(LSSecondCommentCell *)commentCell withConversationCommentCell:(ConversationCommentCell *)ConversationCommentCell{
    
    if ([LSAccountArchive isLogin]) {
        [self.customView.msgTextView becomeFirstResponder];
        self.customView.msgTextView.placeholder = [NSString stringWithFormat:@"回复 %@...",secondCommentModel.UserDisName];
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        self.history_Y_offset = [commentCell convertRect:commentCell.bounds toView:window].origin.y;
        
        self.seletedCellHeight = cellHeight;
        [self keyboardWillShow];
//        if (operationView) {
//            if (operationView.isShowing) {
//                operationView.show = NO;
//            }
//        }
        _topicId = secondCommentModel.TopicId;
        _beCid = secondCommentModel.UserCid;
        _schemeId = secondCommentModel.Id;
        
    }else{
        
        LSLoginVController *loginVC=[[LSLoginVController alloc]init];
        loginVC.presentVCName = @"ConverSationVC";
        [self presentRootViewController:loginVC animated:YES completion:nil];
        loginVC.loginSuccessBlock = ^{
            [self hideKeyboard];
        };
        
    }
    
}


- (void)commentActionWithCell:(ConversationCommentCell *)cell{
    NSIndexPath * indexPath = [_tableView indexPathForCell:cell];
    CommentIssueModel * model = self.commentArray[indexPath.row];
    self.customView.msgTextView.placeholder = [NSString stringWithFormat:@"评论 %@...",model.UserDisName];
    [self.customView.msgTextView becomeFirstResponder];
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.history_Y_offset = [cell convertRect:cell.bounds toView:window].origin.y;
    [self keyboardWillShow];
    self.seletedCellHeight = 0;
    
}
- (void)reportActionWithCell:(ConversationCommentCell *)cell{
    
    [MBProgressHUD showOnlyMessage:@"举报成功" afterDelay:1.5];
    
}
- (void)keyboardWillShow{
    
    CGFloat delta = 0.0;
    if (self.seletedCellHeight){//点击某行row，进行回复某人
        delta = self.history_Y_offset - ([UIApplication sharedApplication].keyWindow.bounds.size.height - 273-self.seletedCellHeight-20);
    }else{//点击评论按钮
        delta = self.history_Y_offset - ([UIApplication sharedApplication].keyWindow.bounds.size.height - 273-100);//24为评论按钮高度，10为评论按钮上部 的5加评论按钮下部的5
    }
    CGPoint offset = _tableView.contentOffset;
    offset.y += delta;
    if (offset.y < 0) {
        offset.y = 0;
    }
    [_tableView setContentOffset:offset animated:YES];
}


#pragma mark -- 查看更多
- (void)reloadTableViewIndexRow:(NSIndexPath *)indexPath{
    CommentIssueModel * model = self.commentArray[indexPath.row];
    model.shouldUpdateCache = YES;
//    [self load1CommentData:NO withType:2 with:model.TopicId with:model.Id WithIndexPath:indexPath withModel:model];
    [_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//    [_tableView reloadData];
    
   
}

- (void)load1CommentData:(BOOL)refresh withType:(NSInteger)type with:(NSInteger)topid with:(NSInteger )Id WithIndexPath:(NSIndexPath *) indexPath withModel:(CommentIssueModel * )model withPageCount:(NSInteger)pageCount{
    _secPageCount = pageCount;
    
    MyLog(@"%ld",_secPageCount);
    if (refresh) {
        _secPageCount = 1;
    }else{
        
        _secPageCount += 1;
    }
    NSDictionary * dict = @{@"tid":@(topid),@"pageIndex":@(_secPageCount),@"type":@(type),@"schemeId":@(Id)};
//    MyLog(@"%@",dict);
    [LSHttpEnclosureTool GET:LSTOPCOMMENTLISt parameters:dict resBlock:^(id responseObject) {
        if (responseObject) {
            MyLog(@"%@",responseObject);
            [self.commentArray1 removeAllObjects];
            for (NSDictionary * dict in responseObject[@"CommentList"]) {
                LSSecondCommentModel * model = [LSSecondCommentModel mj_objectWithKeyValues:dict];
                [self.commentArray1 addObject:model];
            }
            [_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        }else{
            _secPageCount -= 1;
        }
        MyLog(@"%ld",self.commentArray1.count);
        model.pageCount = _secPageCount;
        model.shouldUpdateCache = YES;
        NSArray * array =  [model.SecondCommentList arrayByAddingObjectsFromArray:self.commentArray1];
        model.SecondCommentList = array.mutableCopy;
//        MyLog(@"modelArr= %@",model.SecondCommentList);
//        [_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
//        [_tableView reloadData];
    }];
    
}

/**评论点赞*/
#pragma mark -- 评论点赞
- (void)thumbupActionWithIndexPath:(NSIndexPath *)indexPath{
    if ([LSAccountArchive isLogin]) {
        CommentIssueModel * model = self.commentArray[indexPath.row];
        self.scrollIndexPath = indexPath;
        [self commentOnTopic:1 withId:model.Id withModel:model];
    }else{
        LSLoginVController *loginVC=[[LSLoginVController alloc]init];
        loginVC.presentVCName = @"ConverSationVC";
        [self presentRootViewController:loginVC animated:YES completion:nil];
        loginVC.loginSuccessBlock = ^{
            [self hideKeyboard];
        };
    }
    
    
}

/**点赞接口*/
#pragma mark -- 点赞接口
- (void)commentOnTopic:(NSInteger )type withId:(NSInteger )Id withModel:(id)model{
    
    NSDictionary * dict = @{@"id":@(Id),@"cid":[LSAccountArchive account].Cid,@"type":@(type)};
    MyLog(@"%@",dict);
    [LSHttpEnclosureTool POST:LSTOPICTHUMBUP parameters:@{@"id":@(Id),@"cid":[LSAccountArchive account].Cid,@"type":@(type)} resBlock:^(id responseObject) {
        if (responseObject) {
//            [self loadData:YES];
            if (type == 0) {
//                [self loadData:NO];
                //话题点赞
                ConversationIssueModel * cModel = (ConversationIssueModel *)model;
                
                cModel.ThumbupCount = [responseObject integerValue];
                [_tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
                
            }else{
                //话题某条评论点赞
                CommentIssueModel * cModel = (CommentIssueModel *)model;
                cModel.ThumbupCount = [responseObject integerValue];
                [_tableView reloadSections:[NSIndexSet indexSetWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
                
//                [self loadCommentData:YES withType:1 withScrollCurrentIndex:YES];
            }
            [notifiCenter postNotificationName:@"LOADCIRCLEDATA" object:nil];
            [MBProgressHUD showOnlyMessage:@"点赞成功" afterDelay:1.5];
        }
    }];
    
}


#pragma mark -- ConversationDetailCellDelegate 话题详情代理
#pragma mark -- 话题关注
- (void)gzButtonSender:(UIButton *)sender{
    
    if ([LSAccountArchive isLogin]) {
        ConversationIssueModel  * model = self.conversModelArray[0];
        [self attentionAction:model.UserCid with:sender];
    }else{
        LSLoginVController *loginVC=[[LSLoginVController alloc]init];
        loginVC.presentVCName = @"ConverSationVC";
        [self presentRootViewController:loginVC animated:YES completion:nil];
        loginVC.loginSuccessBlock = ^{
            [self hideKeyboard];
        };
    }
    
    
}
#pragma mark --话题点赞
- (void)thumbupAction:(UIButton *)sender{
    
    if ([LSAccountArchive isLogin]) {
        sender.selected = !sender.selected;
        ConversationIssueModel  * model = self.conversModelArray[0];
        [self commentOnTopic:0 withId:model.TopicId withModel:model];
    }else{
        LSLoginVController *loginVC=[[LSLoginVController alloc]init];
        loginVC.presentVCName = @"ConverSationVC";
        [self presentRootViewController:loginVC animated:YES completion:nil];
        loginVC.loginSuccessBlock = ^{
            [self hideKeyboard];
        };
        
    }
}

#pragma mark -- 关注接口
- (void)attentionAction:(NSString *)Cid with:(UIButton *)sender{
    [LSHttpEnclosureTool POST:LSUserAttention parameters:@{@"cid":Cid,@"fanCid":[LSAccountArchive account].Cid} resBlock:^(id responseObject) {
        //        MyLog(@"%@",responseObject);
        if ([responseObject integerValue] == 1) {
        }
        sender.selected = !sender.selected;
        [notifiCenter postNotificationName:@"LOADCIRCLEDATA" object:nil];
    }];
    
}


#pragma mark - 滚动,点击等相关处理
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//    [[NSNotificationCenter defaultCenter]postNotificationName:@"ChatKeyboardShouldResignFirstResponder" object:nil];
    [self hideKeyboard];
}

- (void)hideKeyboard{
    
    
    [notifiCenter postNotificationName:UIKeyboardWillHideNotification object:@(2)];
//    [notifiCenter addObserver:self.customView selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
}

- (void)sendTextMessage:(NSString *)text{
    if([[text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0) {
        [MBProgressHUD showOnlyMessage:@"评论的内容不能全是空格" afterDelay:1.5];
        return;
    }
    if ([CommonClass isBlankString:text]) {
        [MBProgressHUD showOnlyMessage:@"请输入评论的内容" afterDelay:1.5];
        return;
    }
    
    [self topicCommentWithTopicId:_topicId WithBeCid:_beCid withCid:[LSAccountArchive account].Cid withContent:text withSchemeId:_schemeId];
    [self hideKeyboard];
}


#pragma mark -- 话题评论
- (void)topicCommentWithTopicId:(NSInteger)topicId WithBeCid:(NSString* )beCid withCid:(NSString *)cid withContent:(NSString *)content withSchemeId:(NSInteger )schemeId{
    NSDictionary * dict;
    if (schemeId == 0) {
        dict = @{@"topicId":@(topicId),@"beCid":_beCid,@"cid":cid,@"content":content,@"schemeId":@""};
    }else{
      dict = @{@"topicId":@(topicId),@"beCid":_beCid,@"cid":cid,@"content":content,@"schemeId":@(schemeId)};
    }
    
    MyLog(@"%@",dict);
    
    [LSHttpEnclosureTool POST:LSTOPICCOMMENT parameters:dict resBlock:^(id responseObject) {
        [self loadCommentData:YES withType:1 withIsFirstLoad:NO];
    }];
    
    
}


- (void)presentLoginVC{
    
    LSLoginVController *loginVC=[[LSLoginVController alloc]init];
    [self presentRootViewController:loginVC animated:YES completion:nil];
    loginVC.loginSuccessBlock = ^{
//        _tokenStr = [LSAccountArchive account].Cid;
    };
}

- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}


//- (void)OnlyLookAtTheBuildingAction:(UIButton *)sender{
//
////        sender.selected = !sender.selected;
//        if (sender.selected) {
//            _type = 1;
//            [self loadCommentData:YES withType:1];
//        }else{
//            MyLog(@"取消选中");
//            _type = 2;
//            [self loadCommentData:YES withType:2];
//        }
//
//}

/*右边的分享按钮*/
-(void)shareIssueDetailSender:(UIBarButtonItem *)sender{
    
    DXShareView *shareView = [[DXShareView alloc] init];
    [shareView showShareView];
    
    shareView.ChooseShareTypeBlock = ^(NSInteger type) {
        
        
        switch (type) {
            case DXShareTypeWechatSession://微信好友
            {
                MyLog(@"分享给微信好友");
//                [self shareToWeChat:0];
            }
                break;
            case DXShareTypeWechatTimeline://微信朋友圈
            {
                MyLog(@"分享到微信朋友圈");
//                [self shareToWeChat:1];
            }
                break;
            case DXShareTypeQQ://QQ好友
            {
                MyLog(@"分享给QQ好友");
//                [self shareToQQFriend];
            }
                break;
            case DXShareTypeQzone://QQ空间
            {
                MyLog(@"分享到QQ空间");
//                [self shareToQzone];
            }
                break;
                //        case DXShareTypeUrl://复制链接
                //        {
                //            UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
                //            pasteboard.string = self.shareConentType == DXShareContentTypeLink ? self.shareModel.url : self.shareModel.title;
                //            [XHToast showBottomWithText:@"复制成功"];
                //        }
                //            break;
            default:
                break;
        }
    };
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
